package daoImpls;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import com.sun.org.apache.bcel.internal.generic.CPInstruction;

import dao.IpageDao;
import vo.PetObject;

public class PageDaoImpl extends ObjectDAOImpl implements IpageDao{
	
	private int init(String sql,int pageSize){
		int total = 0, totalPage = 0;
        Connection c = cp.getConnection();
        try ( PreparedStatement p = c.prepareStatement(sql)){
        	ResultSet rs = p.executeQuery();
        	if(rs.next()){
        		total = rs.getInt(1);
        		totalPage = total % pageSize > 0 ? total / pageSize + 1 : total / pageSize; // 总页数
        	}
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } 
        // 总数量
        cp.returnConnection(c);
        return totalPage;
		
	}

	//用户表
	public int getUserTotal(int pageSize) {
        String sql = "select count(uid) from user";
        return this.init(sql, pageSize);
	}
	//商品表
	@Override
	public int getOrderTotal(int pageSize) {
		String sql = "select count(oid) from `order`";
		return this.init(sql, pageSize);
	}
	//订单表
	@Override
	public int getGoodTotal(int pageSize) {
		String sql = "select count(gid) from good";
		return this.init(sql, pageSize);
	}

	@Override
	public int getOrderItemTotal(int pageSize) {
		
		return 0;
	}
	

}
